package com.hd.trans.framework.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.StyleRes;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.hd.trans.R;
import com.hd.trans.utils.ScreenUtil;


/**
 * @author qianjujun
 * @email qianjujun@163.com
 * @createTime 2019/5/29 15:35
 * @describe
 */
public class CstDialog<T extends ViewDataBinding> implements View.OnClickListener {

    protected final Context mContext;
    protected final Dialog dialog;
    protected T mDataBinding;
    private float scaleWidth = 1.0f;

    private DialogInterface.OnDismissListener onDismissListener;
    private int dodgeDismissListenerEnable = 0;

    public CstDialog(Context context, View contentView, @StyleRes int themeResId){
        this(context,contentView,true,true,themeResId);
    }

    public CstDialog(Context context, View contentView, boolean cancelable, boolean touchOutsideCancelable){
        this(context,contentView,cancelable,touchOutsideCancelable, R.style.myDialog);
    }

    public CstDialog(Context context, View contentView, boolean cancelable){
        this(context,contentView,cancelable,cancelable);
    }

    public CstDialog(Context context, @LayoutRes int layoutId, boolean cancelable){
        this(context, LayoutInflater.from(context).inflate(layoutId,null),cancelable,cancelable);
    }

    public CstDialog(Context context, @LayoutRes int layoutId, boolean cancelable, boolean touchOutsideCancelable){
        this(context, LayoutInflater.from(context).inflate(layoutId,null),cancelable,touchOutsideCancelable);
    }

    public CstDialog(Context context, View contentView, boolean cancelable, boolean touchOutsideCancelable, @StyleRes int themeResId) {
        mDataBinding = DataBindingUtil.bind(contentView);
        this.mContext = context;
        dialog = createDialog(context,themeResId);
        dialog.setCancelable(cancelable);
        dialog.setCanceledOnTouchOutside(touchOutsideCancelable);
        dialog.setContentView(contentView);
        dialog.setOnDismissListener(_dialog -> {
            CstDialog.this.onDismiss();
            if (onDismissListener != null){
                boolean isDodge = dodgeDismissListenerEnable > 0;
                if (isDodge){
                    dodgeDismissListenerEnable --;
                }
                else
                    onDismissListener.onDismiss(dialog);
            }
        });
    }

    public View getContentView(){
        return mDataBinding.getRoot();
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener listener) {
        onDismissListener = listener;
    }

    public void setCancelable(boolean cancelable){
        dialog.setCancelable(cancelable);
    }

    public void setCanceledOnTouchOutside(boolean touchOutsideCancelable){
        dialog.setCanceledOnTouchOutside(touchOutsideCancelable);
    }

    protected float dimAmount = -1f;

    protected void onShowed(){

    }

    protected void onDismiss() {

    }

    protected Dialog createDialog(Context context,@StyleRes int themeResId) {
        return new MyDialog(context, themeResId);
    }

    protected Dialog createDialog(Context context) {
        return this.createDialog(context, R.style.myDialog);
    }


    public CstDialog(@NonNull Context context, @NonNull View contentView) {
        this(context, contentView, true);
    }

    public void showFull(){
        showFull(true);
    }

    public void showFull(boolean fullStatusBar) {
        Window window = getWindow();
        if (window != null && !isShowing()) {
            if (fullStatusBar){
                //透明状态栏
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                if (Build.VERSION.SDK_INT >= 28) {
                    window.getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                }
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
                window.setStatusBarColor(Color.TRANSPARENT);
            }

            //全屏
            window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

            showDialog();
        }
    }

    public void show() {
        Window window = getWindow();
        if (window != null && !isShowing()) {
            window.setGravity(Gravity.CENTER);
//            ((Activity) mContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
            window.setLayout(windowWidth(), windowHeight());//设置横向

            showDialog();
        }
    }

    private void showDialog(){
        if (dialog != null && !isShowing()) {
            try {
                dialog.show();
                onShowed();
            }catch (Exception ignored){ }
        }
    }

    public void dismiss() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
            }
        }catch (Exception ignored){ }
    }

    public void dismissNotListener(){
        dodgeDismissListenerEnable = 1;
        dismiss();
    }

    public void setScaleWidth(float scale){
        scaleWidth = scale;
    }

    protected int windowWidth(){
        return (int) (ScreenUtil.getScreenWidth() * scaleWidth);
    }

    protected int windowHeight(){
        return ViewGroup.LayoutParams.WRAP_CONTENT;
    }

    public void showFromBottom() {
        Window window = getWindow();
        if (window != null && !isShowing()) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            window.setGravity(Gravity.BOTTOM);
            WindowManager.LayoutParams atrrs = window.getAttributes();
            atrrs.width = windowWidth();
            atrrs.height = windowHeight();
            window.setAttributes(atrrs);
            window.setWindowAnimations(R.style.BottomAnimStyle);
            showDialog();
        }
    }


    public boolean isShowing() {
        return dialog != null && dialog.isShowing();
    }

    private float mDimAmountCache = -1f;

    public Window getWindow(){
        if (dialog == null)
            return null;
        Window window = dialog.getWindow();
        if (window != null){
            float dimAmount = this.dimAmount;
            if (mDimAmountCache != dimAmount){
                mDimAmountCache = dimAmount;
                WindowManager.LayoutParams params = window.getAttributes();
                if (dimAmount == 0f){
                    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                }
                else if (dimAmount > 0){
                    window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    params.dimAmount = dimAmount;
                }
            }
        }
        return window;
    }

    @Override
    public void onClick(View v) {
        onViewClick(v);
    }

    public void onViewClick(@NonNull View v) {

    }

    protected void onBackPressed(){
        if (dialog != null && dialog.isShowing()){
            ((MyDialog) dialog).superOnBackPressed();
        }
    }

    private class MyDialog extends Dialog {

        public MyDialog(@NonNull Context context, int themeResId) {
            super(context, themeResId);
        }

        public void superOnBackPressed(){
            super.onBackPressed();
        }

        @Override
        public void onBackPressed() {
            CstDialog.this.onBackPressed();
        }
    }

}
